package com.apache.member.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.member.common.ConstantData;
import com.apache.member.entity.MemberRole;
import com.apache.member.entity.MemberType;
import com.apache.member.manager.MemberRoleManager;
import com.apache.member.manager.MemberTypeManager;

@Controller
@RequestMapping("/admin/memberRole")
public class MemberRoleController {

	@Autowired
	private MemberRoleManager memberRoleManager;

	@Autowired
	private MemberTypeManager memberTypeManager;

	@RequestMapping("/delete")
	@ResponseBody
	public ResultMsg delete(String memberId, String typeNo) {
		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setFlag(ConstantData.FLAG_FALSE);
		resultMsg.setMsg("操作失败");
		MemberRole memberRole = new MemberRole();
		memberRole.setMemberId(memberId);
		memberRole.setRoleEname(typeNo);
		ParamsVo<MemberRole> vo = new ParamsVo<MemberRole>();
		vo.setObj(memberRole);
		List<MemberRole> list = memberRoleManager.getList(vo);
		if (list != null && list.size() > 0) {
			for (MemberRole memberRole2 : list) {
				ParamsVo<MemberRole> vo1 = new ParamsVo<MemberRole>();
				vo1.setInfoId(memberRole2.getRoleId());
				boolean deleteInfo = memberRoleManager.deleteInfo(vo1);
				if (deleteInfo) {
					resultMsg.setFlag(ConstantData.FLAG_OK);
					resultMsg.setMsg("操作成功");
				}
			}
		}

		return resultMsg;

	}

	//@Token(remove = true)
	//防表单重复提交注解
	@RequestMapping("/save")
	@ResponseBody
	public ResultMsg save(String memberId, String typeNo) {
		ParamsVo<MemberType> vo = new ParamsVo<MemberType>();
		vo.setInfoId(typeNo);
		MemberType memberType = (MemberType) memberTypeManager.getInfoById(vo);
		ResultMsg resultMsg = new ResultMsg();
		MemberRole memberRole = new MemberRole();
		memberRole.setMemberId(memberId);
		memberRole.setRoleEname(typeNo);
		memberRole.setRoleCname(memberType.getTypeName());
		ParamsVo<MemberRole> vo1 = new ParamsVo<MemberRole>();
		vo1.setObj(memberRole);
		String saveInfo = memberRoleManager.saveInfo(vo1);
		if (saveInfo != null) {
			resultMsg.setFlag(ConstantData.FLAG_OK);
			resultMsg.setMsg("操作成功！");
		}
		return resultMsg;
	}

	/**
	 * 根据memberId获取该会员的role。
	 * description:  
	 * @param memberId
	 * @param request
	 * @param response
	 * @return  
	 * @author cbqiu 
	 * @update 2016-3-22
	 */
	@RequestMapping("/list.action")
	@ResponseBody
	public Map<String, Object> list(MemberRole memberRole, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> mapData = new HashMap<String, Object>();
		ParamsVo<MemberRole> vo = new ParamsVo<MemberRole>();
		vo.setObj(memberRole);
		List<MemberRole> list = memberRoleManager.getList(vo);
		mapData.put("rows", list);

		return mapData;
	}

}
